<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">编辑借阅</div>
    <el-form  :model="form" style="width: 800px" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="图书标准码" prop="bookNo">
        <el-select v-model="form.bookNo" clearable filterable placeholder="请选择" @change="selBook">
          <el-option
              v-for="item in books"
              :key="item.id"
              :label="item.bookNo"
              :value="item.bookNo">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图书名称" prop="bookName">
        <el-input v-model="form.bookName" disabled></el-input>
      </el-form-item>
      <el-form-item label="所需积分" prop="score">
        <el-input v-model="form.score" disabled></el-input>
      </el-form-item>
      <el-form-item label="会员码" prop="userNo">
        <el-select v-model="form.userNo" filterable placeholder="请选择" @change="selUser">
          <el-option
              v-for="item in users"
              :key="item.id"
              :label="item.username"
              :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户姓名" prop="userName">
        <el-input disabled v-model="form.userName"></el-input>
      </el-form-item>
      <el-form-item label="用户联系方式" prop="userPhone">
        <el-input disabled v-model="form.userPhone" ></el-input>
      </el-form-item>

      <div style="text-align: center; margin-top: 30px">
        <el-button type="primary" @click="update" size="medium">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import request from "@/utils/request";
import router from "@/router";

export default {
  name: "EditBorrow",
  data(){
    return{
      users:[],
      books:[],
      rules:{
        bookNo: [
          { required: true, message: '请输入图书标准码', trigger: 'blur'}
        ],
        userNo: [
          { required: true, message: '请输入会员码', trigger: 'blur'}
        ]
      },
      form:{

      }
    }
  },
  created() {
    this.initUsers()
    this.initBooks()
    var id = this.$route.query.id;
    request.get("/borrow/"+id).then(res=>{
      if(res.code=='200'){
        this.form=res.data
      }
    })
  },
  methods:{
    selUser(){
      var user=this.users.find(v=>v.username==this.form.userNo)
      this.form.userName=user.name;
      this.form.userPhone=user.phone;
    },
    selBook(){
      var book=this.books.find(v=>v.bookNo==this.form.bookNo)
      this.form.bookName=book.name;
      this.form.score=book.score;
    },
    initUsers(){
      request.get('/user/list').then(res=>{
        if(res.code=='200'){
          this.users=res.data
        }
      })
    },
    initBooks(){
      request.get('/book/list').then(res=>{
        if(res.code=='200'){
          this.books=res.data
        }
      })
    },
    update(){
      this.$refs['ruleForm'].validate((valid) => {
        if(valid){
          request.put("/borrow/",this.form).then(res=>{
            if(res.code=='200'){
              this.$notify.success("修改成功");
              this.$router.push('/borrowList')
            }else{
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>